<template>
  <div>
    <!-- 面包屑导航 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>队列管理</el-breadcrumb-item>
    </el-breadcrumb>

    <!-- 搜索筛选 -->
    <el-form :inline="true" class="user-search">
      <el-form-item label="搜索：">
      </el-form-item>
      <el-form-item label="" v-for="(filter, index) in searchParams.filters" :key="index">
        <el-input size="small" v-model="filter.fieldValue" v-bind:fieldName="filter.fieldName" v-if="filter.showAble"
                  v-bind:fieldValue="filter.fieldValue" v-bind:compare="filter.compare" :disabled="filter.readonly"
                  @keyup.enter.native="search"
                  :placeholder="filter.placeholder" :readonly="filter.readonly" clearable/>
      </el-form-item>
      <el-form-item>
        <el-button size="small" type="primary" icon="el-icon-search" @click="search()">搜索</el-button>
        <el-button size="small" type="primary" icon="el-icon-refresh" @click="handleAdd()" :loading="isSyncing">同步
        </el-button>
      </el-form-item>
    </el-form>

    <!--列表-->
    <el-table size="small" :data="list" highlight-current-row v-loading="loading" border
              element-loading-text="拼命加载中"
              style="width: 100%;" :header-row-style="{width: '100%', color: 'darkorange'}">
      <el-table-column align="center" type="selection">
      </el-table-column>
      <el-table-column sortable prop="queueName" label="队列名称">
      </el-table-column>
      <el-table-column sortable prop="updateAt" label="最后更新时间">
      </el-table-column>
      <el-table-column align="center" label="操作">
        <template slot-scope="scope">
          <el-button size="mini" type="primary" icon="el-icon-view" @click="showSeat(scope.$index, scope.row)">查看坐席
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页组件 -->
    <Pagination v-bind:child-msg="pageParams" @callFather="callFather"/>

    <!-- 查看坐席-->
    <el-dialog :title="'查看坐席'" :visible.sync="seatDialog.showAble" width="50%" @click='closeDialog()'>
      <el-table size="small" :data="seatDialog.list" highlight-current-row v-loading="seatDialog.loading"
                ref="multipleTable"
                element-loading-text="拼命加载中"
                style="width: 100%;" :header-row-style="{width: '100%', color: 'darkorange'}">
        <el-table-column sortable prop="agentname" label="坐席名称">
        </el-table-column>
        <el-table-column sortable prop="agentnum" label="坐席号">
        </el-table-column>
        <el-table-column sortable prop="updateAt" label="最后更新时间">
        </el-table-column>
      </el-table>
      <!-- 分页组件 -->
      <Pagination v-bind:child-msg="seatDialog.pageParams" @callFather="callSeatFather"/>
    </el-dialog>

  </div>
</template>

<script>
  import {queueList, queueSync, seatList} from "../../../api/baseMG";
  import Pagination from '../../../components/Pagination'
  import {deptAll} from "../../../api/userMG";
  import {err} from "../../../api/cti";

  export default {
    data() {
      return {
        list: [],
        isSyncing: false,
        loading: false,
        editForm: {},
        seatDialog: {
          list: [],
          showAble: false,
          loding: false,
          pageParams: {
            currentPage: 1,
            pageSize: 10,
            total: 10
          },
          searchParams: {
            page: 1,
            perPage: 10,
            filters: [
              {
                showAble: true,
                readonly: false,
                placeholder: "请输入队列名称",
                fieldName: "queueId",
                compare: "=",
                fieldValue: null
              }
            ],
            isLock: ''
          }
        },
        // 分页参数
        pageParams: {
          currentPage: 1,
          pageSize: 10,
          total: 10
        },
        searchParams: {
          page: 1,
          perPage: 10,
          filters: [
            {
              showAble: true,
              readonly: false,
              placeholder: "请输入队列名称",
              fieldName: "queueName",
              compare: "like",
              fieldValue: ""
            }
          ],
          isLock: ''
        }
      }
    },
    created() {
      this.search();
    },
    // 注册组件
    components: {
      Pagination
    },
    methods: {
      closeDialog() {
        this.seatDialog.showAble = false;
      },
      handleAdd() {
        this.$confirm('这个过程可能需要一些时间,确定要同步吗?', '信息', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          this.isSyncing = true;
          queueSync({}).then(resp => {
            if (resp.success) {
              this.$message.success(resp.msg);
            } else {
              this.$message.error(resp.msg);
            }
            this.isSyncing = false;
          }).catch(err => {
            this.$message.error("同步信息失败");
            this.isSyncing = false;
          })
        }).catch(err => {
          this.$message({
            type: 'info',
            message: '已取消同步'
          })
        })
      },
      // 分页插件事件
      callFather(param) {
        this.searchParams.page = param.currentPage;
        this.searchParams.perPage = param.pageSize;
        this.getList(this.searchParams);
      },
      callSeatFather(param) {
        this.seatDialog.searchParams.page = param.currentPage;
        this.seatDialog.searchParams.perPage = param.pageSize;
        this.getSeatList(this.searchParams);
      },
      showSeat(index, row) {
        // this.$router.push({path: "/base/seat/index", query: {queueId: row.id}})
        this.seatDialog.showAble = true;
        this.seatDialog.searchParams.filters = [
          {
            showAble: true,
            readonly: false,
            placeholder: "请输入队列名称",
            fieldName: "queueId",
            compare: "=",
            fieldValue: row.id
          }];
        this.getSeatList();
      },
      getSeatList() {
        seatList(this.seatDialog.searchParams).then(resp => {
          this.seatDialog.list = resp.data.list;
          this.seatDialog.pageParams.currentPage = resp.data.pageNum;
          this.seatDialog.pageParams.pageSize = resp.data.pageSize;
          this.seatDialog.pageParams.total = resp.data.total;
        })
      },
      search() {
        this.getList(this.searchParams);
      },
      getList(params) {
        queueList(params).then(resp => {
          if (resp.success) {
            this.list = resp.data.list;
            this.pageParams.currentPage = resp.data.pageNum;
            this.pageParams.pageSize = resp.data.pageSize;
            this.pageParams.total = resp.data.total;
          }
        })
      },
      deleteItem(index, row) {
        this.$confirm('确定要删除吗?', '信息', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          pstnDelete(row.id).then(res => {
            if (res.success) {
              this.$message({
                type: 'success',
                message: res.msg
              });
              this.search(this.formInline)
            } else {
              this.$message({
                type: 'info',
                message: res.msg
              })
            }
          })
            .catch(err => {
              this.loading = false;
              this.$message.error('删除失败，请稍后再试！')
            })
        }).catch(err => {
          console.error(err);
          this.$message({
            type: 'info',
            message: '已取消删除'
          })
        })
      }
    }
  }
</script>

<style scoped>
  .user-search {
    margin-top: 20px;
  }
</style>
